package com.moxiao.yfjh.module.blind.controller.admin.memberlabel;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;

import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;

import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.moxiao.yfjh.module.blind.controller.admin.memberlabel.vo.*;
import com.moxiao.yfjh.module.blind.dal.dataobject.memberlabel.MemberLabelDO;
import com.moxiao.yfjh.module.blind.convert.memberlabel.MemberLabelConvert;
import com.moxiao.yfjh.module.blind.service.memberlabel.MemberLabelService;

@Tag(name = "管理后台 - 相亲标签库")
@RestController
@RequestMapping("/blind/member-label")
@Validated
public class MemberLabelController {

    @Resource
    private MemberLabelService memberLabelService;

    @PostMapping("/create")
    @Operation(summary = "创建相亲标签库")
    @PreAuthorize("@ss.hasPermission('blind:member-label:create')")
    public CommonResult<Long> createMemberLabel(@Valid @RequestBody MemberLabelCreateReqVO createReqVO) {
        return success(memberLabelService.createMemberLabel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新相亲标签库")
    @PreAuthorize("@ss.hasPermission('blind:member-label:update')")
    public CommonResult<Boolean> updateMemberLabel(@Valid @RequestBody MemberLabelUpdateReqVO updateReqVO) {
        memberLabelService.updateMemberLabel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲标签库")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blind:member-label:delete')")
    public CommonResult<Boolean> deleteMemberLabel(@RequestParam("id") Long id) {
        memberLabelService.deleteMemberLabel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲标签库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blind:member-label:query')")
    public CommonResult<MemberLabelRespVO> getMemberLabel(@RequestParam("id") Long id) {
        MemberLabelDO memberLabel = memberLabelService.getMemberLabel(id);
        return success(MemberLabelConvert.INSTANCE.convert(memberLabel));
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲标签库列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blind:member-label:query')")
    public CommonResult<List<MemberLabelRespVO>> getMemberLabelList(@RequestParam("ids") Collection<Long> ids) {
        List<MemberLabelDO> list = memberLabelService.getMemberLabelList(ids);
        return success(MemberLabelConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/getMemberLabel")
    @Operation(summary = "获得相亲标签库分页")
    @PreAuthorize("@ss.hasPermission('blind:member-label:query')")
    public CommonResult<List<MemberLabelRespVO>> getMemberLabel() {
        List<MemberLabelDO> memberLabelList = memberLabelService.getMemberLabelList(new MemberLabelExportReqVO());
        List<MemberLabelRespVO> memberLabelRespVOS = MemberLabelConvert.INSTANCE.convertList(memberLabelList);
        memberLabelRespVOS.forEach(memberLabelRespVO -> {
            memberLabelRespVO.setLabelId(memberLabelRespVO.getId());
        });
        return success(memberLabelRespVOS);
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲标签库分页")
    @PreAuthorize("@ss.hasPermission('blind:member-label:query')")
    public CommonResult<PageResult<MemberLabelRespVO>> getMemberLabelPage(@Valid MemberLabelPageReqVO pageVO) {
        PageResult<MemberLabelDO> pageResult = memberLabelService.getMemberLabelPage(pageVO);
        return success(MemberLabelConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲标签库 Excel")
    @PreAuthorize("@ss.hasPermission('blind:member-label:export')")
    @OperateLog(type = EXPORT)
    public void exportMemberLabelExcel(@Valid MemberLabelExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MemberLabelDO> list = memberLabelService.getMemberLabelList(exportReqVO);
        // 导出 Excel
        List<MemberLabelExcelVO> datas = MemberLabelConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲标签库.xls", "数据", MemberLabelExcelVO.class, datas);
    }

}
